/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mapping;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
/**
 *
 * @author duonghau
 */
public class SqlLib {
    private Connection connection=null;
    private Statement statement=null;
    private ResultSet resultset=null;
    private String databaseURL="jdbc:mysql://localhost:3306/GestionClinic";
    private String databaseUserName="root";
    private String databasePassWord="";
    public boolean initConnection(){
	try {
            Class.forName("com.mysql.jdbc.Driver");
	} catch (ClassNotFoundException e) {
            return false;
	}        
        try{
            connection=DriverManager.getConnection(databaseURL,databaseUserName, databasePassWord);
            return true;
        }catch(SQLException e){
            System.out.print(e.getMessage());
            return false;
        }
    }
    public boolean closeConnection(){
        try{
            connection.close();
            return true;
        }catch(SQLException e){
            return false;
        }        
    }
    //select
    public ResultSet getDataFromQuery(String sqlQuery){
        if(connection!=null){
            try{
                statement=connection.createStatement();
                resultset=statement.executeQuery(sqlQuery);
                return resultset;
            }catch(SQLException e){
                System.out.print(e.getMessage());
                return null;   
            }
        }
        else{
            return null;
        }
    }
    //update va delete
    public boolean execQuery(String sqlQuery){
        if(connection!=null){
            try{
                statement=connection.createStatement();
                statement.executeUpdate(sqlQuery);
                return true;
            }catch(SQLException e){
                System.out.print(e.getMessage());
                return false;   
            }
        }
        else{
            return false;
        }
    } 
}